package populus.core.queue;

import java.util.BitSet;

import populus.utils.HashAlgorithms;
import populus.utils.MD5;

/**
 * 布隆过滤器
 * @author burkun
 *
 */
public class BoomFilter {
	
	private int hashFunNum = 7; //默认使用7个
	private static final int MAX_SIZE = Integer.MAX_VALUE;
	
	private BitSet flags = new BitSet(MAX_SIZE);
	private int[] prime =  new int[]{10079, 10091 , 10093, 10151, 10163, 10181, 10211, 10243, 20107, 20113, 20117};
	
	public boolean containAndAdd(UrlItem url){
		//如果不在自动加入
		int[] indexs = new int[hashFunNum];
		boolean isIn = false;
		for(int i=0; i<hashFunNum; i++){
			indexs[i] = HashAlgorithms.additiveHash(MD5.getMD5Code(url.getUrl()), prime[i]);
			isIn |= flags.get(indexs[i]);
		}
		if(!isIn){
			for(int i=0; i < hashFunNum; i++){
				flags.set(indexs[i]);
			}
		}
		return isIn;
	}
	
	
	
	
}
